package org.jeecg.modules.flow.sys.service.impl;

/*
 * 项目名称:    wms-server-jeecg
 * 接口描述:    //
 * 创建人员:    zhaojun
 * 创建时间:    2023-12-05
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.business.delivery.entity.WmsDeliveryTask;
import org.jeecg.modules.business.delivery.service.IWmsDeliveryTaskService;
import org.jeecg.modules.business.doc.business.entity.WmsInReceipt;
import org.jeecg.modules.business.doc.business.service.IWmsInReceiptService;
import org.jeecg.modules.business.doc.outbound.entity.WmsOutReceipt;
import org.jeecg.modules.business.doc.outbound.service.IWmsOutReceiptService;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTask;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskService;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceipt;
import org.jeecg.modules.business.transfer.service.IWmsMoveReceiptService;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTask;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskService;
import org.jeecg.modules.flow.sys.service.DocumentInfoServce;
import org.jeecg.modules.flow.sys.util.ClassUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor
public class DocumentInfoServceImpl implements DocumentInfoServce {

    private final IWmsMoveReceiptService wmsMoveReceiptService;
    @Lazy
    private final IWmsDeliveryTaskService wmsDeliveryTaskService;
    private final IWmsInReceiptService wmsInReceiptService;
    private final IWmsInReceiptTaskService wmsInReceiptTaskService;
    @Lazy
    private final IWmsOutReceiptTaskService wmsOutReceiptTaskService;
    private final IWmsOutReceiptService wmsOutReceiptService;

    @Override
    public Map<String, Object> getDocumentInfo(String documentTye, String infoId) {

        Map<String, Object> map = new HashMap<>();

        Map<String, Object> classMap = new HashMap<>();
        String documentCode = "";
        try {
            switch (documentTye) {
                case "document_task":
                    WmsInReceiptTask inReceiptTask = wmsInReceiptTaskService.getOne(
                            new LambdaQueryWrapper<WmsInReceiptTask>().eq(WmsInReceiptTask::getId, infoId));
                    if (ObjectUtils.isNotEmpty(inReceiptTask)) {
                        documentCode = inReceiptTask.getDocumentCode();
                        classMap = ClassUtil.convertToMap(inReceiptTask);
                    }
                    break;
                case "document_type":
                    WmsInReceipt InReceipt = wmsInReceiptService.getOne(
                            new LambdaQueryWrapper<WmsInReceipt>().eq(WmsInReceipt::getId, infoId));
                    if (ObjectUtils.isNotEmpty(InReceipt)) {
                        documentCode = InReceipt.getDocumentCode();
                        classMap = ClassUtil.convertToMap(InReceipt);
                    }
                    break;
                case "outbound_task":
                    WmsOutReceiptTask receiptTask = wmsOutReceiptTaskService.getOne(
                            new LambdaQueryWrapper<WmsOutReceiptTask>().eq(WmsOutReceiptTask::getId, infoId));
                    if (ObjectUtils.isNotEmpty(receiptTask)) {
                        documentCode = receiptTask.getDocumentCode();
                        classMap = ClassUtil.convertToMap(receiptTask);
                    }
                    break;
                case "outbound_type":
                    WmsOutReceipt outReceipt = wmsOutReceiptService.getOne(
                            new LambdaQueryWrapper<WmsOutReceipt>().eq(WmsOutReceipt::getId, infoId));
                    if (ObjectUtils.isNotEmpty(outReceipt)) {
                        documentCode = outReceipt.getDocumentCode();
                        classMap = ClassUtil.convertToMap(outReceipt);
                    }
                    break;
                case "move_type":
                    WmsMoveReceipt moveReceipt = wmsMoveReceiptService.getOne(
                            new LambdaQueryWrapper<WmsMoveReceipt>().eq(WmsMoveReceipt::getId, infoId));
                    if (ObjectUtils.isNotEmpty(moveReceipt)) {
                        documentCode = moveReceipt.getDocumentCode();
                        classMap = ClassUtil.convertToMap(moveReceipt);
                    }
                    break;
                case "delivery_task":
                    WmsDeliveryTask deliveryTask = wmsDeliveryTaskService.getOne(
                            new LambdaQueryWrapper<WmsDeliveryTask>().eq(WmsDeliveryTask::getId, infoId));
                    if (ObjectUtils.isNotEmpty(deliveryTask)) {
                        documentCode = deliveryTask.getDocumentCode();
                        classMap = ClassUtil.convertToMap(deliveryTask);
                    }
                    break;
                default:
                    throw new JeecgBootException("代码中未处理当前流程的逻辑，请管理员联系开发添加");
            }
            map.put("documentCode", documentCode);
            map.put("classMap", classMap);
        } catch (Exception e) {
            log.error("解析异常,异常信息：{}", e.getMessage());
        }
        return map;
    }
}
